package com.china.svc.comprehensivetest.baseGather.base;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

/**
 * 所有Activity的基类，所有的Activity都要继承此类
 * 封装：查看子控件 初始化数据   设置监听器
 * toast ,showDialog,showProgressDialog
 */
public abstract class BaseActivity extends AppCompatActivity
        implements IUIOperation {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayoutRes());
//



        initView();
        initListener();
        initData();
    }

    /**
     * Find child controls ignore wall tiles.
     */
    public <T> T findView(int id) {
        T view = (T) findViewById(id);
        return view;
    }

    public void showToast(String text) {
        Global.showToast(text);
    }

    private ProgressDialog mPDialog;

    /**
     * display loading prompt box (cannot be called in child thread)
     */
    protected void showProgressDialog(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mPDialog = new ProgressDialog(BaseActivity.this);
                mPDialog.setMessage(message);
                //点击外部 不能销毁提示窗
                mPDialog.setCanceledOnTouchOutside(false);
                // activity如果正在销毁或已销毁，不能show Dialog，否则出错。
                if (!isFinishing())
                    mPDialog.show();
            }
        });
    }

//    protected void setPageTitle(String title) {
//        TextView tvTitle = findView(R.id.tv_title);
//        if (tvTitle != null) {
//            tvTitle.setText(title);
//        }
//  }

    /**
     * 销毁加载提示框
     */
    protected void dismissProgressDialog() {
        if (mPDialog != null) {
            mPDialog.dismiss();
            mPDialog = null;
        }
    }

    /**
     * 显示提示框
     *
     * @param title    标题
     * @param message  内容
     * @param listener 回调监听器
     */
    protected void showDialog(String title, String message,
                              final OnDialogClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (listener != null) {
                            listener.onConfirm(dialog);
                        }
                    }
                });
        builder.setNegativeButton("取消", null);
        if (!isFinishing())
            builder.create().show();
    }

    /**
     * 提示对话框1
     */
    protected void showDialog(String title, String message) {
        showDialog(title, message, null);
    }

    /**
     * 提示对话框2
     */
    protected void showDialog(String message) {
        showDialog("", message, null);
    }

    /**
     * 对话框回调
     */
    public interface OnDialogClickListener {

        /**
         * 确定
         */
        void onConfirm(DialogInterface dialog);

        /**
         * 取消
         */
        void onCancel(DialogInterface dialog);
    }
}



















